package com.example.service.appointment;


import cn.hutool.core.date.DateUtil;
import com.example.common.enums.ReservationStatusEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.exception.CustomException;
import com.example.mapper.appointment.MedicalCardMapper;
import com.example.mapper.appointment.RecordMapper;
import com.example.mapper.appointment.ReservationMapper;
import com.example.mapper.appointment.UserMapper;
import com.example.mapper.medical.DoctorMapper;
import com.example.model.entity.appointment.MedicalCard;
import com.example.model.entity.appointment.Record;
import com.example.model.entity.appointment.Reservation;
import com.example.model.entity.appointment.User;
import com.example.model.entity.medical.Doctor;
import com.example.model.entity.other.Account;
import com.example.model.vo.ReservationVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 预约信息表业务处理
 **/
@Service
public class ReservationService {

    @Resource
    private ReservationMapper reservationMapper;
    @Resource
    private RecordMapper recordMapper;
    @Resource
    private MedicalCardMapper medicalCardMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 新增
     */
    @Transactional
    public void add(Reservation reservation) throws ParseException {
        if (reservation.getAppointmentDate() == null) {
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            reservation.setAppointmentDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format));
        }
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null) {
            if (RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
                reservation.setDoctorId(currentUser.getId());
                Doctor doctor = doctorMapper.selectById(currentUser.getId());
                doctor.setCount(doctor.getCount()+1);
                doctorMapper.updateById(doctor);
                reservation.setStatus(ReservationStatusEnum.STATUS_SUCCESS.getName());
            }
            if (RoleEnum.HOSPITAL.name().equals(currentUser.getRole())){
                reservation.setDoctorId(reservation.getDoctorId());
                Doctor doctor = doctorMapper.selectById(reservation.getDoctorId());
                doctor.setCount(doctor.getCount()+1);
                doctorMapper.updateById(doctor);
                reservation.setStatus(ReservationStatusEnum.STATUS_SUCCESS.getName());
            }
            if (RoleEnum.USER.name().equals(currentUser.getRole())){
                reservation.setUserId(currentUser.getId());
                User user = userMapper.selectById(currentUser.getId());
                double account = user.getAccount();
                if (account < reservation.getPrice()) {
                    throw new CustomException(ResultCodeEnum.PAY_EXIT_NO);
                }
                user.setAccount(account - reservation.getPrice());
                userMapper.updateById(user);
                reservation.setStatus(ReservationStatusEnum.STATUS_ING.getName());
            }
            reservationMapper.insert(reservation);
            Record record = new Record();
            if (Objects.equals(reservation.getStatus(), ReservationStatusEnum.STATUS_SUCCESS.getName())) {
                record.setUserId(reservation.getUserId());
                record.setReservationId(reservation.getId());
                recordMapper.insert(record);
                MedicalCard medicalCard = new MedicalCard();
                medicalCard.setRecordId(record.getId());
                medicalCardMapper.insert(medicalCard);
            }
        }else{
            reservationMapper.insert(reservation);

        }
    }
    /**
     * 修改
     */
    public void updateById(Reservation reservation) {
        if (Objects.equals(reservation.getStatus(), ReservationStatusEnum.STATUS_SUCCESS.getName())) {
            reservationMapper.updateById(reservation);
            Record record=new Record();
            record.setUserId(reservation.getUserId());
            record.setReservationId(reservation.getId());
            recordMapper.insert(record);
            MedicalCard medicalCard = new MedicalCard();
            medicalCard.setRecordId(record.getId());
            medicalCardMapper.insert(medicalCard);
        }

    }


    /**
     * 删除
     */
    public void deleteById(Integer id) {
        reservationMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            reservationMapper.deleteById(id);
        }
    }


    /**
     * 根据ID查询
     */
    public Reservation selectById(Integer id) {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ReservationVO> selectAll(Reservation reservation) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.HOSPITAL.name().equals(currentUser.getRole())){
            reservation.setHospitalId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            reservation.setDoctorId(currentUser.getId());
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            reservation.setUserId(currentUser.getId());
        }
        return reservationMapper.selectAll(reservation);
    }

    /**
     * 分页查询
     */
    public PageInfo<ReservationVO> selectPage(Reservation reservation, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.HOSPITAL.name().equals(currentUser.getRole())){
            reservation.setHospitalId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            reservation.setDoctorId(currentUser.getId());
        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            reservation.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<ReservationVO> list = reservationMapper.selectAll(reservation);
        return PageInfo.of(list);
    }

}
